﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRM.Utility;
using HRM.Entity;
using HRM.BIZ;

namespace WebUI.Pages
{
    public partial class EmployeeRewardManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string type = Request.QueryString["type"];
                ddlType.SelectedValue = type;
                LoadData();
            }
        }

        protected void LoadData()
        {
            string empID = Request.QueryString["id"];

            EmployeeRewardBiz empBiz = new EmployeeRewardBiz();
            var result = empBiz.GetByEmployee(empID.ToInt32(), ddlType.SelectedValue.ToInt32());

            grvEmployeeReward.DataSource = result;
            grvEmployeeReward.DataBind();

            Employee emp = new EmployeeBiz().GetByID(empID.ToInt32());
            if (emp != null)
            {
                ucEmpNavigate.RecordID = emp.ID;
                ucEmpNavigate.EmployeeName = emp.Name;
            }
        }

        protected void grvEmployeeReward_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chkAll = new CheckBox();
                chkAll.ID = "chkAll";
                e.Row.Cells[0].Controls.Add(chkAll);
            }
        }

        protected void grvEmployeeReward_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chk = e.Row.FindControl("chkAll") as CheckBox;
                if (chk != null)
                {
                    chk.Attributes.Add("onclick", string.Format("selectAll('{0}')", chk.ClientID));
                }
            }
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void grvEmployeeReward_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "EditItem":
                    {
                        int id = e.CommandArgument.ToInt32();
                        EmployeeRewardBiz empBiz = new EmployeeRewardBiz();
                        var result = empBiz.GetByID(id);

                        btnAction.CommandArgument = id.ToString();
                        btnAction.CommandName = "Edit";
                        btnAction.Text = "Lưu";

                        ddlAddType.SelectedValue = result.Type.ToString();
                        txtContent.Text = result.Content;
                        divAction.Style["display"] = "block";
                        break;
                    }
                case "DeleteItem":
                    {
                        int id = e.CommandArgument.ToInt32();
                        EmployeeRewardBiz empBiz = new EmployeeRewardBiz();
                        empBiz.Delete(id);
                        LoadData();
                        break;
                    }
            }
          
        }

        protected void grvEmployeeReward_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvEmployeeReward.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            btnAction.CommandName = "Add";
            btnAction.CommandArgument = "";
            btnAction.Text = "Thêm";
            txtContent.Text = "";
            divAction.Style["display"] = "block";
        }

        protected void btnAction_Click(object sender, EventArgs e)
        {
            switch (btnAction.CommandName)
            {
                case "Add":
                    {
                        EmployeeRewardBiz empBiz = new EmployeeRewardBiz();
                        EmployeeReward item = new EmployeeReward();
                        item.Content = txtContent.Text;
                        item.Type = ddlAddType.SelectedValue.ToInt32();
                        item.EmpID = Request.QueryString["id"].ToInt32();

                        empBiz.Add(item);
                        (Master as DefaultMasterPage).RunStartupScript("displayMessage('Thêm thành công')");
                        divAction.Style["display"] = "none";
                        LoadData();
                        break;
                    }
                case "Edit":
                    {
                        EmployeeRewardBiz empBiz = new EmployeeRewardBiz();
                        EmployeeReward item = empBiz.GetByID(btnAction.CommandArgument.ToInt32());
                        item.Content = txtContent.Text;
                        item.Type = ddlAddType.SelectedValue.ToInt32();
                        
                        empBiz.Update(item);
                        (Master as DefaultMasterPage).RunStartupScript("displayMessage('Lưu thành công')");
                        divAction.Style["display"] = "none";
                        LoadData();
                        break;
                    }
            }
        }
    }
}
